//
//  SheepLayer.cpp
//  Sheep
//
//  Created by Tran Tho on 9/8/14.
//
//

#include "SheepLayer.h"
#include "GameScene.h"
#include "SimpleAudioEngine.h"
#include "config.h"

bool SheepLayer::init()
{
    if(!Layer::init())
    {
        return false;
    }
    
    
    _size = Director::getInstance()->getWinSize();
    
    count = 0;
    
    type = 0;
    
    listSheep = new Vector<Sheep*>();
    listPic = new Vector<Pic*>();
    
    addSheep();
    
    auto ro0 = Ro::create();
    ro0->setPosition(Vec2(_size.width - ro0->getContentSize().width * 0.4 / 2, 250));
    this->addChild(ro0,9999);
    
    Label* w0 = Label::createWithTTF("Dog","Marker Felt.ttf", 32);
    w0->setColor(Color3B(255, 0, 0));
    w0->setPosition(_size.width - 50, 270);
    this->addChild(w0,9999);
    
    return true;
}

void SheepLayer::update(float dt)
{
    if(!GameScene::getInstance()->isGameOver && GameScene::getInstance()->gameStatus == play)
        actionSheep();
    
}

void SheepLayer::onEnter()
{
    Layer::onEnter();
    this->scheduleUpdate();
}

void SheepLayer::onExit()
{
    Layer::onExit();
    this->unscheduleUpdate();
    cleanUp();
    this->removeAllChildrenWithCleanup(true);
}

void SheepLayer::addSheep()
{
    
    auto sheep = Sheep::create();
    sheep->setPosition(_size.width  * 1 / 8, _size.height * 3 / 4);
    sheep->setTYPE(type);
    this->addChild(sheep, 1000);
    listSheep->pushBack(sheep);
    CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(SOUND_BEE);
    
    auto pic = Pic::create(type);
    pic->setPosition(sheep->getPositionX(), sheep->getPositionY() - pic->getContentSize().height);
    this->addChild(pic);
    listPic->pushBack(pic);
    
    type++;
}

void SheepLayer::actionSheep()
{
    
    
    for(auto _sheep : *listSheep)
    {
        
        for(auto _pic :*listPic)
        {
            if(_pic->getTYPE() == _sheep->getTYPE())
                _pic->setPosition(_sheep->getPositionX(), _sheep->getPositionY() - _pic->getContentSize().height);
        }
    
        if(_sheep->getPositionY() <= _size.height / 4 + _sheep->getContentSize().height / 2  && _sheep->getPositionX() <= _size.width)
        {
             CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(SOUND_HIT);
            GameScene::getInstance()->isGameOver = true;
            
        }
        
        if(_sheep->velocY <= 0)
        {
            _sheep->velocY = 0.0;
            _sheep->directionY = 1;
            _sheep->sheepStaus = isNormal;
            //_sheep->runAction(RotateTo::create(0.6, 30));
        }
        
        if(_sheep->getPositionY() <= GameScene::getInstance()->land->getContentSize().height + _sheep->getContentSize().height / 2)
        {
           
            _sheep->setPositionY(GameScene::getInstance()->land->getContentSize().height + _sheep->getContentSize().height /2 );
        }
        
        _sheep->velocY += _sheep->directionY * _sheep->getDeltaA();
        
        _sheep->setPositionY(_sheep->getPositionY() - _sheep->velocY * _sheep->directionY);
        _sheep->setPositionX(_sheep->getPositionX() + 1.0 * _sheep->directionX);
    }
    
   
}

void SheepLayer::cleanUp()
{
    for (auto _sheep : *listSheep )
    {
        listSheep->eraseObject(_sheep);
    
    }
    
    delete listSheep;
    listSheep = nullptr;
    
}

void SheepLayer::getTapAction(cocos2d::Vec2 pos)
{
    
    
    for(auto _sheep : *listSheep)
    {
        
        _sheep->setTapLocation(pos);
        
        if(_sheep->isTap(_sheep->getPosition()) && _sheep->sheepStaus == isNormal)
        {
            CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(SOUND_CLICK);
            _sheep->sheepStaus = isTap;
            _sheep->directionY = -1;
            _sheep->velocY = 15.0;
            //_sheep->runAction(RotateTo::create(0.2, -30));
            count++;
            
            if(count == 3)
                addSheep();
            
//            if(count == 7)
//            {
//                addSheep();
//            }
//            
//            if(count == 11)
//            {
//                addSheep();
//            }
            
        }
    }
}










